package com.example.gustavo.hello.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import com.example.gustavo.hello.R;
import com.example.gustavo.hello.data.Images;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

//照片瀑布流 使用UIL 基本实现,待完善
public class PhotoFallActivity extends BaseActivity {

    private static final String TAG = "PhotoFallActivity";
    private LinearLayout ll_column1,ll_column2,ll_column3;
    private int height1,height2,height3,currentHeight;
    private ScrollView scrollView;
    private int lastPositon;
    private DisplayImageOptions options;
    private boolean loading;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_photo_fall);
    }

    @Override
    protected void initView() {

        scrollView = (ScrollView) findViewById(R.id.sv);
        ll_column1 = (LinearLayout) findViewById(R.id.ll_column1);
        ll_column2 = (LinearLayout) findViewById(R.id.ll_column2);
        ll_column3 = (LinearLayout) findViewById(R.id.ll_column3);

    }

    @Override
    protected void initData() {

        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.ic_launcher)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .imageScaleType(ImageScaleType.EXACTLY)
                .build();

        currentHeight = getResources().getDisplayMetrics().heightPixels;
        loadMoreImage();



    }

    /**
     * 计算缩放比
     * @param width
     * @param reqWidth
     * @return
     */
    private int calcScale(int width,int reqWidth){

        int scale = 1;

        if(width > reqWidth){

            scale = Math.round((float) width / (float) reqWidth);

        }

        return scale;
    }
    @Override
    protected void initEvent() {

        scrollView.setSmoothScrollingEnabled(true);
        scrollView.setOnTouchListener(new View.OnTouchListener() {
            float oldY;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (!loading) {

                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:

                            oldY = event.getY();
                            Log.i(TAG, "start=" + oldY);

                            break;


                        case MotionEvent.ACTION_UP:

                            Log.i(TAG,"getScrollY="+scrollView.getScrollY());
                            float y = oldY - event.getY();

                            if (y > 0) {

                                currentHeight += y;
                                Log.i(TAG, "y=" + y + " currentHeight=" + currentHeight);
                                loadMoreImage();
                            }

                            break;


                    }
                }


                return false;
            }
        });


    }

    private void loadMoreImage(){

        if (height1 < currentHeight && height2 <currentHeight && height3 < currentHeight){
            loading = true;

            ImageView imageView = new ImageView(this);
            imageView.setMaxWidth(ll_column1.getWidth());
            imageView.setPadding(5,5,5,5);
            imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);



            if(height1 <= height2){

                if(height1 <= height3){
                    addAndDisplayView(1, ll_column1, imageView);
                }else{
                    addAndDisplayView(3, ll_column3, imageView);

                }
            }else{

                if(height2 <= height3){
                    addAndDisplayView(2, ll_column2, imageView);
                }else{
                    addAndDisplayView(3, ll_column3, imageView);

                }
            }




        }else{
            loading = false;
        }



    }

    private void addAndDisplayView(final int num, LinearLayout linearLayout, ImageView imageView) {
        Log.i(TAG,"column="+num+" height1="+height1+" height2="+height2+" height3="+height3+" h="+currentHeight+ " image="+lastPositon+" imageView="+imageView);

        ImageLoader.getInstance().displayImage(Images.imageUrls[lastPositon], imageView, options, new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String s, View view) {

            }

            @Override
            public void onLoadingFailed(String s, View view, FailReason failReason) {

            }


            @Override
            public void onLoadingComplete(String s, View view, Bitmap bitmap) {

                switch (num) {
                    case 1:
                        height1 += bitmap.getHeight();
                        break;
                    case 2:
                        height2 += bitmap.getHeight();
                        break;
                    case 3:
                        height3 += bitmap.getHeight();
                        break;
                }


                lastPositon++;
                if (lastPositon >= 52) {
                    lastPositon = 0;
                }
                loadMoreImage();


            }

            @Override
            public void onLoadingCancelled(String s, View view) {

            }
        });

        if (linearLayout != null) {

            linearLayout.addView(imageView);
        }
    }


}
